package com.ftg.product.config;

import com.ftg.product.util.JsonEnum;
import com.ftg.product.util.JsonToWeb;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.HttpClientErrorException;

/**
 * 专门用来做全部 @RestController修饰的类的全局异常的
 * @author kangjx
 * @createTime 2022/8/22
 */
@RestControllerAdvice
public class GloabControllerException {

    @ResponseStatus(value= HttpStatus.BAD_REQUEST)
    public String handleHttpMessageNotReadableException(MissingServletRequestParameterException ex){
        return JsonToWeb.build(JsonEnum.NON_PARAMTER);
    }

    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(value= HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleTypeMismatchException(NullPointerException ex){
        return  JsonToWeb.build(JsonEnum.NULLPOINTER);
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleUnexpectedServer(Exception ex){
        return JsonToWeb.build(JsonEnum.SYS_ERROR);
    }

}
